package com.meixiang.adapter.home;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.StagingBinding;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author yinsujun  Created at 2016/5/5 13:53
 */
public class StagingBindAdapter extends RecyclerView.Adapter<StagingBindAdapter.ViewHolder> {
    private Context context;
    private List<StagingBinding> stagingBindings=new ArrayList<>();
    private int selectPosition=-1;
    public StagingBindAdapter(Context context, List<StagingBinding> stagingBindings) {
        this.context=context;
        this.stagingBindings=stagingBindings;
    }

    public void notifyData(int position){
        this.selectPosition=position;
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_promote_for_binding_list_item, parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if(mOnItemClickListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                    notifyData(pos);

                }
            });
        }
        if(selectPosition==position){
            holder.ivSelect.setVisibility(View.VISIBLE);
            Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.ic_staging_circle);
            holder.ivIcon.setBackground(drawable);
            holder.tvName.setTextColor(ContextCompat.getColor(context,R.color.white));
        }else{
            holder.ivSelect.setVisibility(View.GONE);
            holder.ivIcon.setBackground(null);
            holder.tvName.setTextColor(ContextCompat.getColor(context,R.color.staging_gray_bind_text_color));
        }
        holder.ivIcon.setImageResource(stagingBindings.get(position).getBindIcon());
        holder.tvName.setText(stagingBindings.get(position).getBindName());
    }

    @Override
    public int getItemCount() {
        return stagingBindings.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
//      void onItemLongClick(View view , int position);
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.staging_binding_list_iv_icon)
        ImageView ivIcon;
        @Bind(R.id.staging_binding_list_iv_select)
        ImageView ivSelect;
        @Bind(R.id.staging_binding_list_tv_name)
        TextView tvName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

        }
    }

}
